package com.blq.developtools.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.graphics.Rect;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityNodeInfo;

import blq.ssnb.snbutil.SnbDisplayUtil;

/**
 * <pre>
 * ****************************************************************
 * 文件名称:DevelopUtils.java
 * 作 者:SSNB
 * 创建时间:2024/5/15
 * 文件描述:
 * 注意事项:
 * 版权声明:Copyright (C) 2015-2025 杭州中焯信息技术股份有限公司
 * 修改历史:2024/5/15 1.00 初始版本
 * ****************************************************************
 * </pre>
 */
public class DevelopUtils {


    public static boolean isAccessibilitySettingsOn(Context mContext, Class AccessibilityService) {
        int accessibilityEnabled = 0;
        // TestService为对应的服务
        final String service = mContext.getPackageName() + "/" + AccessibilityService.getCanonicalName();
        try {
            accessibilityEnabled = Settings.Secure.getInt(mContext.getApplicationContext().getContentResolver(),
                    android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException ignored) {
        }
        TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

        if (accessibilityEnabled == 1) {
            String settingValue = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(),
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                mStringColonSplitter.setString(settingValue);
                while (mStringColonSplitter.hasNext()) {
                    String accessibilityService = mStringColonSplitter.next();

                    if (accessibilityService.equalsIgnoreCase(service)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    public static boolean isFloatWindowAllowed(Context context) {
        ApplicationInfo appInfo = context.getApplicationInfo();
        int appTargetSdkVersion = appInfo.targetSdkVersion;
        if (appTargetSdkVersion < android.os.Build.VERSION_CODES.M) {
            return true;
        }
        return Settings.canDrawOverlays(context);
    }


    public static <T> T unNullBean(T d, T def) {
        if (d == null) {
            return def;
        }
        return d;
    }

    public static String FormatResourceName(String resourceName) {
        if (resourceName != null && !resourceName.isEmpty()) {
            StringBuilder returnStr = new StringBuilder();
            String[] split1 = resourceName.split(":");
            if (split1.length == 2) {
                returnStr.append("R.");
                String[] split2 = split1[1].split("/");
                if (split2.length == 2) {
                    returnStr.append(split2[0]).append(".").append(split2[1]);
                }
            }
            return returnStr.toString();
        } else {
            return "";
        }
    }

    public static String FormatPxDp(Context context, int px) {
        return px + "px(" + SnbDisplayUtil.px2Dp(context, px) + "dp)";
    }

    public static String FormatLocation(Context context, Rect location) {
        return "左上角坐标:[" + location.left + "px," + location.top + "px]" + "\n" +
                "即[" + SnbDisplayUtil.px2Dp(context, location.left) + "dp," + SnbDisplayUtil.px2Dp(context, location.top) + "dp]" + "\n" +
                "右下角坐标:[" + location.right + "px," + location.bottom + "px]" + "\n" +
                "即[" + SnbDisplayUtil.px2Dp(context, location.right) + "dp," + SnbDisplayUtil.px2Dp(context, location.bottom) + "dp]";
    }
}
